package cz.muni.fi.pa165.bean;

import cz.muni.fi.pa165.dto.DeliveryDTO;
import cz.muni.fi.pa165.dto.PackageDTO;
import cz.muni.fi.pa165.service.local.impl.DeliveryService;
import cz.muni.fi.pa165.service.local.impl.PackageService;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author <a href="mailto:vramik at redhat.com">Vlastislav Ramik</a>
 */
@SessionScoped
@Named
public class PackageManager implements Serializable{

    @Inject
    DeliveryService deliveryService;
    
    @Inject
    PackageService packageService;
    
    @Inject
    DeliveryManager deliveryManager;
    
    private String addHeight;
    private String addWidth;
    private String addWeight;
    private String addPrice;
    private boolean atLeastOnePackageCreated;

    public String getAddHeight() {
        return addHeight;
    }

    public void setAddHeight(String addHeight) {
        this.addHeight = addHeight;
    }

    public String getAddWidth() {
        return addWidth;
    }

    public void setAddWidth(String addWidth) {
        this.addWidth = addWidth;
    }

    public String getAddWeight() {
        return addWeight;
    }

    public void setAddWeight(String addWeight) {
        this.addWeight = addWeight;
    }

    public String getAddPrice() {
        return addPrice;
    }

    public void setAddPrice(String addPrice) {
        this.addPrice = addPrice;
    }

    public boolean isAtLeastOnePackageCreated() {
        return atLeastOnePackageCreated;
    }

    public void setAtLeastOnePackageCreated(boolean atLeastOnePackageCreated) {
        this.atLeastOnePackageCreated = atLeastOnePackageCreated;
    }

    public void createPackage() {
        atLeastOnePackageCreated = false;
        PackageDTO packageDTO = new PackageDTO();
        packageDTO.setHeight(Integer.parseInt(addHeight));
        packageDTO.setWidth(Integer.parseInt(addWidth));
        packageDTO.setWeight(Integer.parseInt(addWeight));
        packageDTO.setPrice(BigDecimal.valueOf(Double.parseDouble(addPrice)));
        packageService.createPackage(packageDTO);
        
        DeliveryDTO deliveryById = deliveryService.getDeliveryById(deliveryManager.getCreatedDeliveryId());
        List<PackageDTO> packages = deliveryById.getPackages();
        packages.add(packageDTO);
        deliveryById.setPackages(packages);
        deliveryService.updateDelivery(deliveryById);
        atLeastOnePackageCreated = true;
    }
}
